package org.seed.rpc.springboot;

import org.reflections.Reflections;
import org.seed.rpc.protocol.annotation.RpcService;
import org.seed.rpc.transfer.RpcProtocol;
import org.seed.rpc.transfer.RpcProviderProcessor;
import org.smartboot.socket.transport.AioQuickServer;
import java.util.Set;

public class EcRpcServerStarter {


    /**
     * 启动 RPC-Server
     *
     * @param ecRpcProperties 本地RPC-Provider配置
     */
    public static void run(EcRpcProperties ecRpcProperties) throws Exception {

        Reflections reflections = new Reflections(ecRpcProperties.getBasePackage());
        Set<Class<?>> classDeclared = reflections.getTypesAnnotatedWith(RpcService.class);
        RpcProviderProcessor rpcProviderProcessor = new RpcProviderProcessor();
        AioQuickServer server = new AioQuickServer(ecRpcProperties.getPort(), new RpcProtocol(), rpcProviderProcessor);
        server.start();
        for (Class<?> providerClass : classDeclared) {
            RpcService rpcService = providerClass.getAnnotation(RpcService.class);
            String interfaceClassName = rpcService.interfaceClass();
            Class<?> interfaceClass = Class.forName(interfaceClassName);
            rpcProviderProcessor.publishService(interfaceClass, providerClass.getDeclaredConstructor().newInstance());
        }

    }



}
